/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jdesktop.wonderland.modules.hslutemplatecell.client.jme.cellrenderer;

import com.jme.scene.Node;
import org.jdesktop.wonderland.client.cell.Cell;
import org.jdesktop.mtgame.Entity;
import org.jdesktop.wonderland.client.jme.cellrenderer.BasicRenderer;
import com.jme.bounding.BoundingBox;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.mtgame.processor.WorkProcessor.WorkCommit;
import org.jdesktop.wonderland.client.cell.asset.AssetUtils;
import org.jdesktop.wonderland.client.jme.ClientContextJME;
import org.jdesktop.wonderland.client.jme.SceneWorker;
import org.jdesktop.wonderland.client.jme.artimport.DeployedModel;
import org.jdesktop.wonderland.client.jme.artimport.LoaderManager;
import org.jdesktop.wonderland.modules.hslutemplatecell.client.TemplateCell;

/**
 *
 * @author Dominik
 */
public class TemplateCellRenderer extends BasicRenderer {

    private Node node = null;
    private String modulName = "hslu-template-cell";
    private String kmzName = "template";

    public TemplateCellRenderer(Cell cell) {
        super(cell);
    }

    private Node getShapeNode(String name, String shapeType) throws MalformedURLException, IOException {

        URL modelUrl = null;
        LoaderManager manager = LoaderManager.getLoaderManager();
        DeployedModel dm = null;
        Node newNode = null;
               
        modelUrl = AssetUtils.getAssetURL("wla://"+modulName+"/"+kmzName+"_"+shapeType+".kmz/"+kmzName+"_"+shapeType+".kmz.dep", cell);
        dm = manager.getLoaderFromDeployment(modelUrl);
        newNode = dm.getModelLoader().loadDeployedModel(dm, entity);

        return newNode;
    }

    protected Node createSceneGraph(Entity entity) {
        String name = cell.getCellID().toString();
        String shapeType = ((TemplateCell) cell).getShapeType();
        Node mesh = null;
        try {
            mesh = this.getShapeNode(name, shapeType);
        } catch (MalformedURLException ex) {
            Logger.getLogger(TemplateCellRenderer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TemplateCellRenderer.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (mesh == null) {
            node = new Node();
            return node;
        }
        node = new Node();
        node.attachChild(mesh);
        node.setModelBound(new BoundingBox());
        node.updateModelBound();
        node.setName("Cell " + cell.getCellID() + ":" + cell.getName());
        return node;
    }

    public void updateShape() {
        final String name = cell.getCellID().toString();
        final String shapeType = ((TemplateCell) cell).getShapeType();
        SceneWorker.addWorker(new WorkCommit() {
            public void commit() {
                node.detachAllChildren();
                try {
                    node.attachChild(getShapeNode(name, shapeType));
                } catch (MalformedURLException ex) {
                    Logger.getLogger(TemplateCellRenderer.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(TemplateCellRenderer.class.getName()).log(Level.SEVERE, null, ex);
                }
                node.setModelBound(new BoundingBox());
                node.updateModelBound();
                ClientContextJME.getWorldManager().addToUpdateList(node);
            }
        });
    }
}
